//
//  MCRechangeCodeVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/5.
//

import UIKit
import SnapKit
import Toast_Swift
import SwiftyJSON
import QMUIKit

class MCRechangeCodeVc: MCBaseVc {
    
    var rechangeTF:UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "兑换码"
        loadSubView()
    }
    
    //UI
    func loadSubView() -> Void {
        let rechangeCodeLabel = QMUILabel.init()
        rechangeCodeLabel.text = "激活你的兑换码"
        rechangeCodeLabel.font = UIFont(name: "PingFangSC-Medium", size: 25)
        rechangeCodeLabel.textColor = UIColor.qmui_color(withHexString: "#333333")
        view.addSubview(rechangeCodeLabel)
        rechangeCodeLabel.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(45)
            make.centerX.equalTo(view.snp.centerX).offset(0)
        }
        
        //兑换码
        let rechangeTextField = QMUITextField.init()
        rechangeTextField.placeholder = "请输入兑换码"
        rechangeTextField.textAlignment = .center
        rechangeCodeLabel.textColor = UIColor.qmui_color(withHexString: "#333333")
        rechangeCodeLabel.font = UIFont(name: "PingFangSC-Semibold", size: 16)
        rechangeTextField.keyboardType = .asciiCapable
        view.addSubview(rechangeTextField)
        rechangeTextField.snp.makeConstraints { make in
            make.top.equalTo(rechangeCodeLabel.snp.bottom).offset(50)
            make.left.equalTo(view.snp.left).offset(16)
            make.right.equalTo(view.snp.right).offset(-16)
            make.height.equalTo(60)
        }
        
        rechangeTF = rechangeTextField
        
         
        
        //线
        let lineView = UIView.init()
        lineView.backgroundColor = UIColor.qmui_color(withHexString: "#9C9FAB")?.withAlphaComponent(0.5)
        view.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.top.equalTo(rechangeTextField.snp.bottom).offset(5)
            make.left.equalTo(rechangeTextField.snp.left).offset(20)
            make.right.equalTo(rechangeTextField.snp.right).offset(-20)
            make.height.equalTo(1)
        }
        
        let selectBtn = QMUIButton.init()
        selectBtn.setTitle("激活", for: .normal)
        selectBtn.setTitleColor(UIColor.qmui_color(withHexString: "10151C"), for: .normal)
        selectBtn.layer.cornerRadius = 22.5
        selectBtn.titleLabel?.font = UIFont(name: "PingFangTC-Semibold", size: 15)
        selectBtn.addTarget(self, action: #selector(setRechangeData), for: .touchUpInside)
        selectBtn.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        view.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.left).offset(0)
            make.right.equalTo(lineView.snp.right).offset(0)
            make.top.equalTo(lineView.snp.bottom).offset(60.13)
            make.height.equalTo(45)
        }
    }
    

    @objc func setRechangeData() -> Void {
        
        if rechangeTF.text?.isEmpty == true {
            self.view.makeToast("激活码不能为空")
        }
        
        NetWorkRequest(MCRechangeVM.rechangeCode(code:rechangeTF.text)) { responseModel in
            
            if responseModel.code == 200 {
                self.view.makeToast("激活成功",position: .center)
            }else {
                self.view.makeToast(responseModel.message,position: .center)
            }
            
        } failureCallback: { ResponseModel in
            self.view.makeToast(ResponseModel.message,position: .center)
        }
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }

}
